package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.VO.CityVO;
import com.ruoyi.business.VO.TopVO;
import com.ruoyi.business.domain.BuAccount;
import com.ruoyi.business.service.IBuAccountService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BuEarningsRecordsMapper;
import com.ruoyi.business.domain.BuEarningsRecords;
import com.ruoyi.business.service.IBuEarningsRecordsService;

/**
 * 用户收益记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-12
 */
@Service
public class BuEarningsRecordsServiceImpl implements IBuEarningsRecordsService
{
    @Autowired
    private BuEarningsRecordsMapper buEarningsRecordsMapper;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IBuAccountService buAccountService;
    /**
     * 查询用户收益记录
     *
     * @param id 用户收益记录主键
     * @return 用户收益记录
     */
    @Override
    public BuEarningsRecords selectBuEarningsRecordsById(Long id)
    {
        return buEarningsRecordsMapper.selectBuEarningsRecordsById(id);
    }

    /**
     * 查询用户收益记录列表
     *
     * @param buEarningsRecords 用户收益记录
     * @return 用户收益记录
     */
    @Override
    public List<BuEarningsRecords> selectBuEarningsRecordsList(BuEarningsRecords buEarningsRecords)
    {
        return buEarningsRecordsMapper.selectBuEarningsRecordsList(buEarningsRecords);
    }

    /**
     * 新增用户收益记录
     *
     * @param buEarningsRecords 用户收益记录
     * @return 结果
     */
    @Override
    public int insertBuEarningsRecords(BuEarningsRecords buEarningsRecords)
    {
        //更新账户
        BuAccount buAccount =buAccountService.selectBuAccountById(buEarningsRecords.getUserId());
        if (buAccount==null){
            buAccount = new BuAccount();
            buAccount.setUserId(buEarningsRecords.getUserId());
            buAccount.setEarningsAmount(buEarningsRecords.getEarningsAmount());
            buAccountService.insertBuAccount(buAccount);
        }else {
            buAccount.setEarningsAmount(buAccount.getEarningsAmount().add(buEarningsRecords.getEarningsAmount()));
            buAccountService.updateBuAccount(buAccount);
        }
        return buEarningsRecordsMapper.insertBuEarningsRecords(buEarningsRecords);
    }

    /**
     * 修改用户收益记录
     *
     * @param buEarningsRecords 用户收益记录
     * @return 结果
     */
    @Override
    public int updateBuEarningsRecords(BuEarningsRecords buEarningsRecords)
    {
        return buEarningsRecordsMapper.updateBuEarningsRecords(buEarningsRecords);
    }

    /**
     * 批量删除用户收益记录
     *
     * @param ids 需要删除的用户收益记录主键
     * @return 结果
     */
    @Override
    public int deleteBuEarningsRecordsByIds(Long[] ids)
    {
        return buEarningsRecordsMapper.deleteBuEarningsRecordsByIds(ids);
    }

    /**
     * 删除用户收益记录信息
     *
     * @param id 用户收益记录主键
     * @return 结果
     */
    @Override
    public int deleteBuEarningsRecordsById(Long id)
    {
        //修改余额
        BuEarningsRecords buEarningsRecords = buEarningsRecordsMapper.selectBuEarningsRecordsById(id);
        BuAccount buAccount = buAccountService.selectBuAccountById(buEarningsRecords.getUserId());
        if (buAccount!=null) {
            buAccount.setEarningsAmount( buAccount.getEarningsAmount().subtract( buEarningsRecords.getEarningsAmount() ) );
            buAccountService.updateBuAccount( buAccount );
        }
        return buEarningsRecordsMapper.deleteBuEarningsRecordsById(id);
    }

    @Override
    public List<TopVO> top(){
        return buEarningsRecordsMapper.top();
    }

    @Override
    public List<CityVO> city(){
        List<CityVO> cityVOList = new ArrayList<>();
        SysDept dept = new SysDept();
        //查部门出来
        dept.setLevel( 3 );
        List<SysDept> selectDeptList = deptService.selectDeptList2(dept);
        for (SysDept sysDept : selectDeptList) {
            CityVO cityVO1 = new CityVO();
            cityVO1.setName(sysDept.getDeptName());
            List<Long> deptIds = new ArrayList<>();
            deptIds.add(sysDept.getDeptId());
            //查子部门
            SysDept dept2 = new SysDept();
            dept2.setParentId(sysDept.getDeptId());
            List<SysDept> deptList = deptService.selectDeptList(dept2);
            for (SysDept sysDept2 : deptList){
                //子部门的id存起来
                deptIds.add( sysDept2.getDeptId() );
            }
            Long[] deptIds2 = deptIds.toArray(new Long[0]);
            CityVO cityVO =buEarningsRecordsMapper.city(deptIds2);
            if (cityVO.getValue()!=null){
                cityVO1.setValue(cityVO.getValue().divide( BigDecimal.valueOf( 10000 ), 2, RoundingMode.HALF_UP));
            }else {
                cityVO1.setValue( BigDecimal.ZERO );
            }
            cityVOList.add( cityVO1 );
        }
        return cityVOList;
    }
}
